﻿using Inovout.Memberships.Models;
using Inovout.Memberships.Repositories;
using Inovout.Security;
using Inovout.Services;
using NHibernate.Proxy;

namespace Inovout.Memberships.Services
{
    public class MembershipUserService : RepositoryService<MembershipUser>, IMembershipUserService
    {
        private IRepositoryService<MembershipUser> membershipUserService;

        public MembershipUserService(IRepositoryService<MembershipUser> membershipUserService)
        {
            this.membershipUserService = membershipUserService;
        }

        protected MembershipUserRepository MembershipUserRepository
        {
            get { return (MembershipUserRepository)base.Repository; }
        }
        public IMembershipUser FindByClaimedIdentifier(ClaimedIdentifier claimedIdentifier)
        {
            var membershipUser = MembershipUserRepository.FindByClaimedIdentifier(claimedIdentifier);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

        private void Load(MembershipUser membershipUser)
        {
            IRepositoryService<Membership> membershipService = ComponentRegistry.Resolve<IRepositoryService<Membership>>();
            IRepositoryService<UserCredential> userCredentialService = ComponentRegistry.Resolve<IRepositoryService<UserCredential>>();
            if (membershipUser.Membership == null)
            {
                membershipUser.Membership = membershipService.FindById(membershipUser.Id.MembershipId);
            }
        }

        public IMembershipUser FindByByUserNameOrEmailForLocal(string userName,string email)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByByUserNameOrEmailForLocal(userName,email);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

        public IMembershipUser FindByUserNameOrEmail(string userName)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByUserNameOrEmail(userName);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }


        public IMembershipUser FindByProviderIdentifier(string provider, string identifier)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByProviderIdentifier(provider, identifier);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

        public IMembershipUser FindByUsingIdentifierForeach(string identifier)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByUsingIdentifierForeach(identifier);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

        //public IMembershipUser FindByEmail(string email)
        //{
        //    MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByEmail(email);
        //    if (membershipUser != null)
        //    {
        //        Load(membershipUser);
        //    }
        //    return membershipUser;
        //}

        public IMembershipUser FindByMembership(IMembership membership)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindByMembership(membership);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

        public IMembershipUser FindBySystemUser(SystemUser systemUser)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindBySystemUser(systemUser);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }


        public IMembershipUser FindBySystemUserName(string name)
        {
            MembershipUser membershipUser = (MembershipUser)MembershipUserRepository.FindBySystemUserName(name);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }


        public IMembershipUser FindById(int id)
        {
            MembershipUser membershipUser = MembershipUserRepository.FindById(id);
            if (membershipUser != null)
            {
                Load(membershipUser);
            }
            return membershipUser;
        }

    }
}
